package com.kitchen.ui;

import com.kitchen.entity.*;
import com.kitchen.service.*;
import com.kitchen.util.FileUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * 控制台用户界面
 * 体现面向对象编程：使用多个服务类协作
 */
public class ConsoleUI {
    private Scanner scanner;
    private DishService dishService;
    private OrderService orderService;
    private ChefService chefService;
    
    public ConsoleUI() {
        this.scanner = new Scanner(System.in);
        this.dishService = new DishService();
        this.orderService = new OrderService();
        this.chefService = new ChefService();
    }
    
    public void start() {
        System.out.println("========================================");
        System.out.println("    欢迎使用餐厅后厨订单管理系统");
        System.out.println("========================================");
        
        while (true) {
            showMainMenu();
            int choice = getIntInput("请选择操作: ");
            
            try {
                switch (choice) {
                    case 1:
                        createOrder();
                        break;
                    case 2:
                        viewOrders();
                        break;
                    case 3:
                        assignChef();
                        break;
                    case 4:
                        completeOrder();
                        break;
                    case 5:
                        viewDishes();
                        break;
                    case 6:
                        viewStatistics();
                        break;
                    case 7:
                        viewChefs();
                        break;
                    case 0:
                        System.out.println("感谢使用，再见！");
                        return;
                    default:
                        System.out.println("无效的选择，请重新输入！");
                }
            } catch (Exception e) {
                System.out.println("操作失败: " + e.getMessage());
                FileUtil.writeLog("操作异常: " + e.getMessage());
            }
            
            System.out.println("\n按回车键继续...");
            scanner.nextLine();
        }
    }
    
    private void showMainMenu() {
        System.out.println("\n========== 主菜单 ==========");
        System.out.println("1. 创建订单");
        System.out.println("2. 查看订单");
        System.out.println("3. 分配厨师");
        System.out.println("4. 完成订单");
        System.out.println("5. 查看菜品");
        System.out.println("6. 查看统计");
        System.out.println("7. 查看厨师");
        System.out.println("0. 退出系统");
        System.out.println("============================");
    }
    
    private void createOrder() {
        System.out.println("\n========== 创建订单 ==========");
        String tableNumber = getStringInput("请输入桌号: ");
        
        // 显示可用菜品
        List<Dish> dishes = dishService.getAllAvailableDishes();
        if (dishes.isEmpty()) {
            System.out.println("暂无可用菜品！");
            return;
        }
        
        System.out.println("\n可用菜品列表:");
        Map<Integer, Dish> dishMap = new HashMap<>();
        for (Dish dish : dishes) {
            dishMap.put(dish.getDishId(), dish);
            System.out.printf("%d. %s (%s) - ¥%.2f - 制作时间: %d分钟\n",
                    dish.getDishId(), dish.getDishName(), dish.getDishType(),
                    dish.getPrice(), dish.getPreparationTime());
        }
        
        // 选择菜品
        Map<Integer, Integer> selectedDishes = new HashMap<>();
        while (true) {
            int dishId = getIntInput("\n请输入菜品ID (输入0结束): ");
            if (dishId == 0) {
                break;
            }
            
            if (!dishMap.containsKey(dishId)) {
                System.out.println("菜品ID不存在！");
                continue;
            }
            
            int quantity = getIntInput("请输入数量: ");
            if (quantity > 0) {
                selectedDishes.put(dishId, selectedDishes.getOrDefault(dishId, 0) + quantity);
                System.out.println("已添加: " + dishMap.get(dishId).getDishName() + " x" + quantity);
            }
        }
        
        if (selectedDishes.isEmpty()) {
            System.out.println("未选择任何菜品！");
            return;
        }
        
        // 创建订单
        Order order = orderService.createOrder(tableNumber, selectedDishes);
        System.out.println("\n订单创建成功！");
        System.out.println("订单号: " + order.getOrderNumber());
        System.out.println("总金额: ¥" + order.getTotalAmount());
    }
    
    private void viewOrders() {
        System.out.println("\n========== 查看订单 ==========");
        System.out.println("1. 查看所有订单");
        System.out.println("2. 查看待处理订单");
        System.out.println("3. 查看制作中订单");
        System.out.println("4. 查看已完成订单");
        
        int choice = getIntInput("请选择: ");
        List<Order> orders;
        
        switch (choice) {
            case 1:
                orders = orderService.getAllOrders();
                break;
            case 2:
                orders = orderService.getOrdersByStatus("待处理");
                break;
            case 3:
                orders = orderService.getOrdersByStatus("制作中");
                break;
            case 4:
                orders = orderService.getOrdersByStatus("已完成");
                break;
            default:
                System.out.println("无效选择！");
                return;
        }
        
        if (orders.isEmpty()) {
            System.out.println("暂无订单！");
            return;
        }
        
        System.out.println("\n订单列表:");
        for (Order order : orders) {
            System.out.println("----------------------------------------");
            System.out.println("订单ID: " + order.getOrderId());
            System.out.println("订单号: " + order.getOrderNumber());
            System.out.println("桌号: " + order.getTableNumber());
            System.out.println("状态: " + order.getStatus());
            System.out.println("总金额: ¥" + order.getTotalAmount());
            System.out.println("下单时间: " + order.getOrderTime());
            if (order.getChefId() != null) {
                System.out.println("负责厨师ID: " + order.getChefId());
            }
            System.out.println("订单明细:");
            for (OrderItem item : order.getItems()) {
                System.out.printf("  - %s x%d (¥%.2f) [%s]\n",
                        item.getDishName(), item.getQuantity(), item.getPrice(), item.getStatus());
            }
        }
    }
    

}

